package cn.jjxx.modules.sys.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 密码验证
 *@Title CheckStrong.java
 *@description TODO
 *@time 2020年8月28日 下午4:43:15
 *@author YAOXIONG
 *@version 1.0
 *
 */
public class CheckStrong {
	/**
	 * 验证密码数据
	 *@Title CheckStrong.java
	 *@description TODO
	 *@time 2020年8月28日 下午4:44:34
	 *@author YAOXIONG
	 *@version 1.0
	 *
	 */
	public static Safelevel PwdSecurityLevel(String pPasswordStr) {
		Safelevel safelevel = Safelevel.VERY_WEAK;
		if (pPasswordStr == null) {
			return safelevel;
		}
		int grade = 0;
		int index = 0;
		char[] pPsdChars = pPasswordStr.toCharArray();

		int numIndex = 0;
		int sLetterIndex = 0;
		int lLetterIndex = 0;
		int symbolIndex = 0;

		for (char pPsdChar : pPsdChars) {
			int ascll = pPsdChar;
			/*
			 * 数字 48-57 A-Z 65 - 90 a-z 97 - 122 !"#$%&'()*+,-./ (ASCII码：33~47)
			 * :;<=>?@ (ASCII码：58~64) [\]^_` (ASCII码：91~96) {|}~
			 * (ASCII码：123~126)
			 */
			if (ascll >= 48 && ascll <= 57) {
				numIndex++;
			} else if (ascll >= 65 && ascll <= 90) {
				lLetterIndex++;
			} else if (ascll >= 97 && ascll <= 122) {
				sLetterIndex++;
			} else if ((ascll >= 33 && ascll <= 47)
					|| (ascll >= 58 && ascll <= 64)
					|| (ascll >= 91 && ascll <= 96)
					|| (ascll >= 123 && ascll <= 126)) {
				symbolIndex++;
			}
		}
		/*
		 * 一、密码长度: 5 分: 小于等于 4 个字符 10 分: 5 到 7 字符 25 分: 大于等于 8 个字符
		 */
		if (pPsdChars.length <= 4) {
			index = 5;
		} else if (pPsdChars.length <= 7) {
			index = 10;
		} else {
			index = 25;
		}
		grade += index;

		/*
		 * 二、字母: 0 分: 没有字母 10 分: 全都是小（大）写字母 20 分: 大小写混合字母
		 */
		if (lLetterIndex == 0 && sLetterIndex == 0) {
			index = 0;
		} else if (lLetterIndex != 0 && sLetterIndex != 0) {
			index = 20;
		} else {
			index = 10;
		}
		grade += index;
		/*
		 * 三、数字: 0 分: 没有数字 10 分: 1 个数字 20 分: 大于 1 个数字
		 */
		if (numIndex == 0) {
			index = 0;
		} else if (numIndex == 1) {
			index = 10;
		} else {
			index = 20;
		}
		grade += index;

		/*
		 * 四、符号: 0 分: 没有符号 10 分: 1 个符号 25 分: 大于 1 个符号
		 */
		if (symbolIndex == 0) {
			index = 0;
		} else if (symbolIndex == 1) {
			index = 10;
		} else {
			index = 25;
		}
		grade += index;
		/*
		 * 五、奖励: 2 分: 字母和数字 3 分: 字母、数字和符号 5 分: 大小写字母、数字和符号
		 */
		if ((sLetterIndex != 0 || lLetterIndex != 0) && numIndex != 0) {
			index = 2;
		} else if ((sLetterIndex != 0 || lLetterIndex != 0) && numIndex != 0
				&& symbolIndex != 0) {
			index = 3;
		} else if (sLetterIndex != 0 && lLetterIndex != 0 && numIndex != 0
				&& symbolIndex != 0) {
			index = 5;
		}
		grade += index;

		/*
		 * 最后的评分标准: >= 90: 非常安全 >= 80: 安全（Secure） >= 70: 非常强 >= 60: 强（Strong） >=
		 * 50: 一般（Average） >= 25: 弱（Weak） >= 0: 非常弱
		 */    
		if(grade >=90){
			safelevel = Safelevel.VERY_SECURE;
		}else if(grade >= 80){
			safelevel = Safelevel.SECURE;
		}else if(grade >= 70){
			safelevel = Safelevel.VERY_STRONG;
		}else if(grade >= 60){
			safelevel = Safelevel.STRONG;
		}else if(grade >= 50){
			safelevel = Safelevel.AVERAGE;
		}else if(grade >= 25){
			safelevel = Safelevel.WEAK;
		}else if(grade >= 0){
			safelevel = Safelevel.VERY_WEAK;
		}
		return safelevel;
	}

	public enum Safelevel {
		VERY_WEAK("VERY_WEAK","非常弱"), /* 非常弱 */
		WEAK("WEAK","弱"), /* 弱 */
		AVERAGE("AVERAGE","一般"), /* 一般 */
		STRONG("STRONG","强"), /* 强 */
		VERY_STRONG("VERY_STRONG","非常强"), /* 非常强 */
		SECURE("SECURE","安全"), /* 安全 */
		VERY_SECURE("VERY_SECURE","非常安全"); /* 非常安全 */

		private String code;
		private String name;
		Safelevel(String code,String name)
		{
			this.code = code;
			this.name = name;
		}
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		} 
	}


	public static String checkPassword(String passwordStr) {
		String regexZ = "\\d*";
		String regexS = "[a-zA-Z]+";
		String regexT = "\\W+$";
		String regexZT = "\\D*";
		String regexST = "[\\d\\W]*";
		String regexZS = "\\w*";
		String regexZST = "[\\w\\W]*";

		if (passwordStr.matches(regexZ) || passwordStr.matches(regexS) || passwordStr.matches(regexT)) {
			return "弱";
		}
		if (passwordStr.matches(regexZT) || passwordStr.matches(regexST) || passwordStr.matches(regexZS)) {
			return "中";
		} 
		if (passwordStr.matches(regexZST)) {
			return "强";
		}
		return passwordStr;

	}

	/**
	 * 返回是否强弱密码
	*@Title CheckStrong.java
	*@description TODO
	*@time 2020年8月31日 下午2:07:31
	*@author YAOXIONG
	*@version 1.0
	*
	 */
	public static boolean checkPasswordSafe(String password){ 
		//String reg = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\\W_!@#$%^&*`~()-+=]+$)(?![0-9\\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\\W_!@#$%^&*`~()-+=]{8,160}$";
		String reg = "^(?!.*\\s)(?!^[\\u4e00-\\u9fa5]+$)(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{8,160}$";
        //Pattern Password_Pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-zA-Z])(.{8,160})$");
        Pattern Password_Pattern = Pattern.compile(reg);
        Matcher matcher = Password_Pattern.matcher(password);
        if (matcher.matches()) {
            return true;//强密码
        }else {
        	if (matcher.matches()) {
                return true;//强密码
            }else {
            	if (matcher.matches()) {
                    return true;//强密码
                }
            }
        }
        return false;
   }
	
	
	public static void main(String[] args) {
		String safel = "gzjjxx@SSO";
		//Safelevel safelevel = PwdSecurityLevel(safel);
		//System.out.println(safelevel.getName());
		//System.out.println( cn.jjxx.shiro.session.config.CheckStrong.checkPassword(safel));
		if (!checkPasswordSafe(safel)) {
            System.out.println("密码长度为6到16位,必须包含字母和数字，字母区分大小写");
        }else {
        	System.out.println("强密码123");
        }
	}
}





